/**
 * 
 */
package com.ekingstar.framework.security;

import org.springframework.security.Authentication;
import org.springframework.security.context.SecurityContextHolder;

import com.ekingstar.framework.domain.User;

/**
 * 
 *
 */
public class UserUtil {

	public static boolean isSuperAdmin(String accountName){
		if (accountName==null || accountName.trim().equals("")){
			return false;
		}else if (accountName.trim().equals("admin")){
			return true;
		}
		return false;
	}
	public static User getCurrentUser(){
		Authentication au = SecurityContextHolder.getContext().getAuthentication();
		if (au == null) return null;
		if (au.getPrincipal() == null) return null;
		if (au != null && au.getPrincipal() instanceof User){
			return (User)au.getPrincipal();
		}else {
			User user = new User();
			user.setName(au.getPrincipal().toString());
			user.setPwd(au.getCredentials() == null?null:au.getCredentials().toString());
			return user;
		}
	}
	public static String getCurrentUserId(){
		User user = getCurrentUser();
		if (user == null) return null;
		return user.getId()==null?user.getName():user.getId();
	}
	public static boolean isLogged(){
//		Authentication au = SecurityContextHolder.getContext().getAuthentication();
//		if (au == null) return false;
//		if (au.getPrincipal() == null) return false;
//		if (au.getPrincipal() instanceof User) return true;
//		return false;
		User user= UserUtil.getCurrentUser();
		if (user!=null&&user.getName()!=null&&!user.getName().trim().equals("")&&!user.getName().trim().equals("roleAnonymous"))
			return true;
		else
			return false;
	}
	
	public static String getUserTpye(){
		Authentication au = SecurityContextHolder.getContext().getAuthentication();
		if (au == null) return "";
		if (au.getPrincipal() == null) return "";
		if (au.getPrincipal() instanceof User) return ((User)au.getPrincipal()).getUserType();
		return "";
	}
	
	
}
